package myschool.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

/**
 * @author oldliu
 * @since 1.0
 */
public class MybatisUtil {
    private static SqlSessionFactory sf;

    static {
        String resource = "mybatis-config.xml";
        Reader reader = null;
        try {

            reader = Resources.getResourceAsReader(resource);
            sf = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private MybatisUtil() {
    }
    public static SqlSession get() {
        return sf.openSession();
    }
    public static void close(SqlSession session) {
        session.commit();
        session.close();
    }
}
